#include "../include/kcmath.h"
#include<iostream>

void kc::KCMatrix::set_row(unsigned _row){
	this->_row = _row;
}

unsigned kc::KCMatrix::get_row(){
	return _row;
}

void kc::KCMatrix::set_column(unsigned _column){
	this->_column = _column;
}

unsigned kc::KCMatrix::get_column(){
	return _column;
}

const kc::KCMatrix kc::KCMatrix::operator+(const kc::KCMatrix &m){
	if (_row != m._row || _column != m._column){
		std::cout << "这两个矩阵不能相加！" << std::endl;
	}
	else{
		kc::KCMatrix temp;
		temp._row = _row;
		temp._column = _column;
		for (int i = 0; i != _row*_column; i++){
			temp.M_data[i] = this->M_data[i] + m.M_data[i];
		}
		return temp;
	}
}

const kc::KCMatrix kc::KCMatrix::operator-(const kc::KCMatrix &m){
	if (_row != m._row || _column != m._column){
		std::cout << "这两个矩阵不能相减！" << std::endl;
	}
	else{
		kc::KCMatrix temp;
		temp._row = _row;
		temp._column = _column;
		for (int i = 0; i != _row*_column; i++){
			temp.M_data[i] = this->M_data[i] - m.M_data[i];
		}
		return temp;
	}
}

const kc::KCMatrix kc::KCMatrix::operator*(const kc::KCMatrix&m){
	if (_column != m._row){
		std::cout << "这两个矩阵不能相乘！" << std::endl;
	}
	else{
		kc::KCMatrix temp;
		int num = 0, m_column = 0;
		temp._row = _row;
		temp._column = m._column;
		for (int i = 0; i != _row; i++){
			for (int j = 0; j != m._column; j++){
				temp.M_data[num] = 0;
				for (int k = i*_column; k!=(i + 1)*_column; k++){
					temp.M_data[num] += M_data[k] * m.M_data[m_column];
					m_column += m._column;
				}
				m_column = j + 1;
				num++;
			}
			m_column = 0;
		}
		return temp;
	}
}

const kc::KCMatrix kc::KCMatrix::operator/(const kc::KCMatrix &m){
	if (m._column != m._row || _column != _row || _column != m._column){
		std::cout << "这两个矩阵不能相除！" << std::endl;
	}
	return (*this)*inverse(m);
}

const kc::KCMatrix& kc::KCMatrix::operator+=(const kc::KCMatrix &m){
	if (_row != m._row || _column != m._column){
		std::cout << "这两个矩阵不能相加！" << std::endl;
	}
	else{
		for (int i = 0; i != _row*_column; i++){
			this->M_data[i] += m.M_data[i];
		}
		return *this;
	}
}

const kc::KCMatrix& kc::KCMatrix::operator-=(const kc::KCMatrix &m){
	if (_row != m._row || _column != m._column){
		std::cout << "这两个矩阵不能相减！" << std::endl;
	}
	else{
		for (int i = 0; i != _row*_column; i++){
			this->M_data[i] -= m.M_data[i];
		}
		return *this;
	}
}

const kc::KCMatrix& kc::KCMatrix::operator*=(const kc::KCMatrix &m){
	if (_column != m._row){
		std::cout << "这两个矩阵不能相乘！" << std::endl;
	}
	else{
		kc::KCMatrix temp;
		int num = 0, m_column = 0;
		temp._row = _row;
		temp._column = m._column;
		for (int i = 0; i != _row; i++){
			for (int j = 0; j != m._column; j++){
				temp.M_data[num] = 0;
				for (int k = i*_column; k!=(i + 1)*_column; k++){
					temp.M_data[num] += M_data[k] * m.M_data[m_column];
					m_column += m._column;
				}
				m_column = j + 1;
				num++;
			}
			m_column = 0;
		}
		*this = temp;
		return *this;
	}
}

const kc::KCMatrix& kc::KCMatrix::operator/=(const kc::KCMatrix &m){
	if (m._column != m._row || _column != _row || _column != m._column){
		std::cout << "这两个矩阵不能相除！" << std::endl;
	}
	else{
		KCMatrix temp = inverse(m);
		return operator*=(temp);
	}
}

std::ostream& kc::operator<<(std::ostream &os, kc::KCMatrix &m){
	for (int i = 0; i != m._row * m._column; i++){
		if ((i + 1) % m._column == 0){
			os << m.M_data[i] << std::endl;
		}
		else{
			os << m.M_data[i] << "\t";
		}
	}
	return os;
}

std::istream& kc::operator>>(std::istream &is, kc::KCMatrix &m){
	for (int i = 0; i != m._row*m._column; i++){
		is >> m.M_data[i];
	}
	return is;
}

const double kc::KCMatrix::det(const kc::KCMatrix &m){
	
}

const kc::KCMatrix kc::KCMatrix::trans(const kc::KCMatrix &m){
	kc::KCMatrix temp;
	temp._row = m._column;
	temp._column = m._row;
	for (int i = 0; i != m._row; i++){
		for (int j = 0; j != m._column; j++){
			temp.M_data[j*m._row + i] = m.M_data[i*m._column + j];
		}
	}
	return temp;
}

const kc::KCMatrix kc::KCMatrix::inverse(const kc::KCMatrix &m){

}